package com.jy.project.controller;

import com.github.pagehelper.PageInfo;
import com.jy.project.domain.User;
import com.jy.project.dto.*;
import com.jy.project.service.DictService;
import com.jy.project.service.UserService;
import com.jy.project.util.ServiceResultUtils;
import com.jy.project.vo.DictVO;
import com.jy.project.vo.UserVO;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/v1")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 新增用户信息
     * */
    @RequestMapping("/user/insertUserDTO")
    @ResponseBody
    public Object insertUserDTO(@Validated  UserDTO dto){
        userService.insertUserDTO(dto);
        return ServiceResultUtils.success(null);
    }

    /**
     * 修改
     * */
    @RequestMapping("/user/updateUserDTO")
    @ResponseBody
    public Object updateUserDTO(@Validated UserUpdateDTO dto){
        userService.updateUser(dto);
        return ServiceResultUtils.success(null);
    }



    /**
     * 显示当前用户列表
     * */
    @RequestMapping("/user/listUserVO")
    @ResponseBody
    public Object listUserVO(UserQueryDTO dto){
        List<UserVO> userVOS = userService.listUserVO(dto);
        return ServiceResultUtils.success(new PageInfo<>(userVOS));
    }

    /**
     * 只显示管理员
     * */
    @RequestMapping("/user/listAdminUserVO")
    @ResponseBody
    public Object listAdminUserVO(UserQueryDTO dto){
        dto.setIsAdmin(1);
        List<UserVO> userVOS = userService.listUserVO(dto);
        return ServiceResultUtils.success(userVOS);
    }


    /**
     * 授权部门admin
     * */
    @RequestMapping("/user/createAdmin")
    @ResponseBody
    public Object createAdmin(@RequestHeader("token")String token, CreateAdminDTO dto){
        userService.createAdminDTO(dto);
        return ServiceResultUtils.success(null);
    }


    /**
     * 授权部门admin
     * */
    @RequestMapping("/user/dept-admin/adopt")
    @ResponseBody
    public Object adopt(@RequestHeader("token")String token, Long id){
        userService.adopt(id);
        return ServiceResultUtils.success(null);
    }

    /**
     * 授权部门admin
     * */
    @RequestMapping("/user/dept-admin/reject")
    @ResponseBody
    public Object reject(@RequestHeader("token")String token, Long id){
        userService.reject(id);
        return ServiceResultUtils.success(null);
    }


    /**
     * 授权部门admin
     * */
    @RequestMapping("/user/dept-admin/list")
    @ResponseBody
    public Object list(@RequestBody UserQueryDTO userQueryDTO,Long userId){
        userQueryDTO.setUserId(userId);
        List<User> users = userService.listUser(userQueryDTO);
        return ServiceResultUtils.success(users);
    }
}
